package main

import (
	"fmt"
	"io/ioutil"
)

func bounded(v int) int {
	if v > 100 {
		return 100
	} else if v < 0 {
		return 0
	} else {
		return v
	}
}

func readFile() {
	const filename = "abc.txt"
	// 读取文件的库函数返回两个值，([]byte, error) 文件内容和出错信息
	if contents, err := ioutil.ReadFile(filename); err != nil {
		fmt.Println(err)
	} else {
		fmt.Printf("%s\n", contents)
	}

	//contents, err := ioutil.ReadFile(filename)
	//if err != nil {
	//	fmt.Println(err)
	//} else {
	//	fmt.Printf("%s\n", contents)
	//}
}

func eval(a, b int, op string) int {
	var result int
	switch op {
	// switch会自动break，除非使用fallthrough
	case "+":
		result = a + b
	case "-":
		result = a - b
	case "*":
		result = a * b
	case "/":
		result = a / b
	default:
		panic("unsupported operator:" + op)
	}
	return result
}

func grade(score int) string {
	g := ""
	switch {
	case score < 0 || score > 100:
		panic(fmt.Sprintf("Wrong score: %d", score)) // 抛出异常
	case score < 60:
		g = "F"
	case score < 80:
		g = "C"
	case score < 90:
		g = "B"
	case score <= 100:
		g = "A"
	}
	return g
}

func forTest() {
	sum := 0
	for i := 1; i <= 100; i++ {
		sum += i
	}
	fmt.Println(sum)
}

func main() {
	readFile()
	//res := eval(1,2, "+")
	//fmt.Println(
	//	grade(0),
	//	grade(59),
	//	grade(99),
	//	grade(78),
	//	res,
	//	)
	forTest()
}
